package com.qef.com.mmovie.activity

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import com.avos.avoscloud.AVObject
import com.orhanobut.logger.Logger
import com.qef.com.mmovie.App
import com.qef.com.mmovie.R
import com.qef.com.mmovie.base.ActivityComponent
import com.qef.com.mmovie.base.BaseActivity
import com.qef.com.mmovie.entity.DataBean
import com.qef.com.mmovie.entity.VVIdeoDetailsEntity
import com.qef.com.mmovie.fragment.laugh_img.LaughImgDetailFragment
import com.qef.com.mmovie.mvp.activity.VideoDetailActivity
import com.qef.com.mmovie.mvp.adapter.MyCommentedAdapter
import com.qef.com.mmovie.mvp.adapter.RecyclerViewAnimator
import com.qef.com.mmovie.presenter.MyCommentedPresenter
import com.qef.com.mmovie.presenter.contract.CollectionContract
import com.qef.com.mmovie.utils.Constants
import com.qef.com.mmovie.utils.GsonUtils
import com.qef.com.mmovie.utils.HindToolbar
import com.qef.com.mmovie.widget.EndLessOnScrollListener
import kotlinx.android.synthetic.main.activity_my_comment.*
import kotlinx.android.synthetic.main.auto_toolbar.*

class MyCommentActivity : BaseActivity<MyCommentedPresenter>(), CollectionContract.View {
    var collections: MutableList<AVObject> = mutableListOf()
    var collectionAdapter: MyCommentedAdapter? = null
    var page: Int = 1
    override fun getLayoutId(): Int {
        HindToolbar.setHintWindowTitle(window)
        return R.layout.activity_my_comment
    }

    override fun initEventAndData() {
        initToolbarNav(toolbar, "我评论的")
        collectionAdapter = MyCommentedAdapter(App.getInstance(), collections)
        mPresenter.getData(1)
        refresh.setProgressViewOffset(false, 100, 200)
        refresh.isRefreshing = true
        val layout = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        refresh.setOnRefreshListener {
            refresh.isRefreshing = true
            page = 1
            mPresenter.getData(page)
        }
        recycler.layoutManager = layout

        collectionAdapter!!.setOnItemClickListener { position, _, _ ->
            val type = collections[position].get(Constants.MyComment.VIDEO_TYPE)
            val realType = type.toString().toInt()
            if (realType == 1) {
                val dataBean = GsonUtils.jsonToBean(collections[position]
                        .get(Constants.Collection.VIDEO_INFO).toString(), DataBean::class.java) as DataBean
                startActivity(Intent(this@MyCommentActivity, VideoDetailActivity::class.java)
                        .putExtra(VideoDetailActivity.VIDEO_INFOS, dataBean))
            } else if (realType == 3) {
                val dataBean = GsonUtils.jsonToBean(collections[position]
                        .get(Constants.Collection.VIDEO_INFO).toString(), VVIdeoDetailsEntity::class.java) as VVIdeoDetailsEntity
                startActivity(Intent(this@MyCommentActivity, LaughImgDetailFragment::class.java)
                        .putExtra("dataBean", dataBean))
            }
        }
        recycler.adapter = RecyclerViewAnimator.setAnimator(collectionAdapter)
        recycler.addOnScrollListener(object : EndLessOnScrollListener(layout, 0) {
            override fun onLoadMore() {
                mPresenter.getData(++page)
            }
        })
        initView()
    }

    private fun initView() {


    }

    override fun initInject(activityComponent: ActivityComponent) {
        activityComponent.inject(this)
    }

    override fun showContent(list: MutableList<AVObject>) {
        Logger.i(list.toString())
        if (refresh.isRefreshing) {
            refresh.isRefreshing = false
            collections.clear()
            collections.addAll(list)
            collectionAdapter!!.clear()
            collectionAdapter!!.addAll(collections)
        } else {
            if (true)
                collectionAdapter?.addAll(list)
        }
        if (true)
            collections.addAll(list)
    }

    override fun error(throwable: Throwable?) {
    }
}
